#include "MatrixGraph.h"
#include<cstring>
void initGraph(MGraph* graph, char* name[], int num, int direction, int edgevalue)
{
	graph->direction = direction;
	graph->nodeNum = num;
	graph->edgeNum = 0;
	memset(graph->edge, 0, sizeof(graph->edge));
	memset(graph->edge, 0, sizeof(MartrixEdge) * maxNodeNum * maxNodeNum);
	for (int i = 0; i < num; i++)
	{
		graph->vex[i].id = i;
		graph->vex[i].show = name[i];
		for (int j = 0; j < num; j++)
		{
			graph->edge[i][j] = edgevalue;
		}
	}


}

static int isEdge(int w)
{
	if (w > 0&&w<1E5)
	{
		return 1;
	}
	return 0;
}

void addMGraphEdge(MGraph* graph, int x, int y, int w)
{
	if (x<0 || x>maxNodeNum || y<0 || y>maxNodeNum)
	{
		return;
	}
	if (!isEdge(graph[x][y]))
	{
		graph->edge[x][y] = w;
		
		if (graph->direction == 0)//�Ƿ�����
		{
			graph->edge[y][x] = w;
		}
		++graph->edgeNum;
	}
}

void visitNode(MatrixVertex* node)
{
	cout << node->show;
}

static int visited[maxNodeNum];

void DFS_MGraph(MGraph* graph, int v)
{
	visited[v] = 1;
	visitNode(graph->vex[v]);
	for (int i = 0; i < graph->nodeNum; i++)
	{
		if (isEdge(graph->edge[v][i]) && !visited[i])
		{
			DFS_MGraph(graph, i);
		}
	}
}
//���ѣ��������
void BFS_MGraph(MGraph* graph, int v)
{
	int que[maxNodeNum];
	int cur;
	int rear = 0, front = 0;
	rear = (rear+ 1)%maxNodeNum;
	que[rear] = v;
	visited[v] = 1;
	while (front != rear)
	{
		front = (front + 1) % maxNodeNum;
		cur = que[front];
		visitNode(&graph->vex[cur]);
		for (int i = 0; i < graph->nodeNum; i++)
		{
			if (isEdge(graph->edge[cur][i] && !visited[i]))
			{
				rear = (rear + 1) % maxNodeNum;
				que[rear] = i;
				visited[i] = 1;
			}
		}
	}
}
